package pe.edu.ups.studio.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import pe.edu.upc.studio.excepcion.DAOExcepcion;
import pe.edu.upc.studio.modelo.Articulo;
import pe.edu.upc.studio.negocio.GestionSalaNegocio;

/**
 * Servlet implementation class ServicioServlet
 */
@WebServlet("/ServicioServlet")
public class ServicioServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ServicioServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String strDes = request.getParameter("txtDescripcion");
		Double dblCosto = Double.parseDouble(request.getParameter("txtCosto"));
		
		GestionSalaNegocio objNeg = new GestionSalaNegocio();
		try {
			objNeg.insertarServicio(strDes, dblCosto, 2);
			Collection<Articulo> lista = new ArrayList<Articulo>();
			lista = objNeg.ListarArticulo(2);
			request.setAttribute("listaservicios", lista);
			RequestDispatcher rd = request.getRequestDispatcher("registroservicios.jsp");
			rd.forward(request, response);
		} catch (DAOExcepcion e) {
			String msj ="No se pudo registrar";
			System.err.println(msj);
			RequestDispatcher rd = request.getRequestDispatcher("/error.jsp?error=" + msj);
			rd.forward(request, response);
		}
	}

}
